package com.tjetc.view.studentview;

import com.tjetc.proxy.CilentProxy;
import com.zhiyun.domain.Student;
import com.zhiyun.domain.User;
import com.zhiyun.service.StudentService;
import com.zhiyun.service.UserService;
import com.zhiyun.vo.AppData;
import com.zhiyun.vo.Result;

import java.util.Scanner;

public class MyInfView_Update {
     Scanner scanner=new Scanner(System.in);
    StudentService studentService= CilentProxy.getProxyInstance(StudentService.class);
    UserService userService=CilentProxy.getProxyInstance(UserService.class);
    public void start() {
        while (true){
            System.out.println("~~~~~~~~~~修改信息界面~~~~~~~~~~");
            System.out.println("1.修改我的个人信息");
            System.out.println("2.修改密码");
            System.out.println("0.返回上一级");
            System.out.print("请输入选择:");
            int i = scanner.nextInt();
            switch (i){
                case 1:
                    updateMyInf();
                    break;
                case 2:
                    updateMyPassWord();
                    break;
                case 0:
                    return;
                default:
                    System.out.println("输入错误,请重新输入");
                    break;
            }

        }
    }
    private void updateMyInf() {
        while (true){
            Result result = studentService.myInfo(AppData.student.getId());
            if (result.getCode()==200){
                Student obj = (Student) result.getObj();
                System.out.println("昵称:"+obj.getNickname());
                System.out.println("姓名:"+obj.getTruename());
                System.out.println("手机号:"+obj.getPhone());
                System.out.println("邮箱:"+obj.getEmail());
                System.out.println("性别:"+obj.getSex());
                System.out.println("生日:"+obj.getBirthday());
                while (true){
                    System.out.print("请输入修改后的昵称:");
                    String nickname = scanner.next();
                    System.out.print("请输入修改后的姓名:");
                    String truename = scanner.next();
                    System.out.print("请输入修改后的手机号:");
                    String phone = scanner.next();
                    System.out.print("请输入修改后的邮箱:");
                    String email = scanner.next();
                    System.out.print("请输入修改后的性别:");
                    String sex = scanner.next();
                    System.out.print("请输入修改后的生日:");
                    String birthday = scanner.next();
                    Student student = new Student(AppData.student.getId(), AppData.student.getUserId(), nickname, truename
                            , phone, email, sex, birthday, AppData.student.getSchool(), AppData.student.getSid(),
                            AppData.student.getUser(), AppData.student.getCourseList());
                    Result result1 = studentService.updateStudent(student);
                    if (result1.getCode()==200){
                        Student obj1 = (Student) result1.getObj();
                        AppData.student.setNickname(nickname);
                        AppData.student.setTruename(truename);
                        AppData.student.setPhone(phone);
                        AppData.student.setEmail(email);
                        AppData.student.setSex(sex);
                        AppData.student.setBirthday(birthday);
                        System.out.println("修改成功~~~~~~~~~~~");
                    }else {
                        System.out.println("修改失败");
                    }
                    break;
                }



            }else {
                System.out.println("身份信息不存在,不能修改~~~");
            }
            break;
        }
    }
    private void updateMyPassWord() {
       while (true){
           System.out.println("请输入原密码");
           String password = scanner.next();
           if (password.equals(AppData.loginUser.getPassword())){
               String newpassword=null;
              while (true){
                  System.out.println("请输入修改后的密码:");
                   newpassword = scanner.next();
                  System.out.println("请再一次输入密码");
                  String next = scanner.next();
                  if (newpassword.equals(next)){
                      break;
                  }else {
                      System.out.println("两次密码输入不相同,请重新输入");
                  }
              }
               Result result = userService.UpdatePassword
                       (AppData.loginUser.getId(),newpassword);
               int code = result.getCode();
               if (code==200){
                   AppData.loginUser.setPassword(newpassword);
                   System.out.println("修改成功");
               }else {
                   System.out.println("修改失败");
               }
               break;
           }else {
               System.out.println("原密码输入错误");
           }

       }
    }
}
